package com.ww.scm.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ww.core.orm.Page;
import com.ww.core.orm.PropertyFilter;
import com.ww.core.utils.AssertUtils;
import com.ww.scm.entity.pub.Dictionary;
import com.ww.scm.entity.pub.DictionaryType;
import com.ww.scm.repository.pub.DictionaryRepository;
import com.ww.scm.repository.pub.DictionaryTypeRepository;
import com.ww.scm.valueObject.dic.DictionaryVo;

//Spring Bean的标识.
@Component
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class DictionaryManager {
	/*
	 * 日志类
	 */
	private static Logger logger = LoggerFactory.getLogger(DictionaryManager.class);

	/*
	 * 定义Repository
	 */
	private DictionaryRepository dictionaryRepository;
	private DictionaryTypeRepository dictionaryTypeRepository;
		
	
	
	/*
	 * 注入Repository
	 */
	@Autowired
	public void setDictionaryRepository(DictionaryRepository dictionaryRepository) {
		this.dictionaryRepository = dictionaryRepository;
	}
	@Autowired
	public void setDictionaryTypeRepository(DictionaryTypeRepository dictionaryTypeRepository) {
		this.dictionaryTypeRepository = dictionaryTypeRepository;
	}
	
	@Transactional(readOnly = true)
	public Dictionary findDictionary(String id){
		return dictionaryRepository.get(id);
	}
	
	@Transactional(readOnly = true)
	public Page<Dictionary> findDictionaryPage(Page<Dictionary> page,List<PropertyFilter> filters){
		Page<Dictionary> dictionaryList=dictionaryRepository.findPage(page,filters);
		return dictionaryList;
	}
	
	@Transactional(readOnly = true)
	public List<Dictionary> findDictionaryList(List<PropertyFilter> filters){
		List<Dictionary> dictionaryList=dictionaryRepository.find(filters);
		return dictionaryList;
	}
	
	@Transactional(readOnly = true)
	public Dictionary findDictionary(List<PropertyFilter> filters){
		Dictionary dictionary=dictionaryRepository.findUnique(filters);
		return dictionary;
	}
	
	@Transactional(readOnly = true)
	public Dictionary findDictionaryById(String id){
		Dictionary dictionary=dictionaryRepository.get(id);
		return dictionary;
	}
	
	public void deleteDictionary(String id,String operator){
		dictionaryRepository.delete(id,operator);
	}
	
	public void saveDictionary(DictionaryVo dictionaryVo){
		AssertUtils.notNull(dictionaryVo, "dictionary不能为空");
		String id=dictionaryVo.getId();
		Dictionary dictionary = new Dictionary();
		if (StringUtils.isNotBlank(id)){
			dictionary =dictionaryRepository.get(id);
		}
		//ConvertUtils.map(dictionaryVo, tagetDictionary);
		dictionary.copyOf(dictionaryVo);
		dictionaryRepository.save(dictionary);
	}
	
	@Transactional(readOnly = true)
	public Page<DictionaryType> findDictionaryTypePage(Page<DictionaryType> page,List<PropertyFilter> filters){
		Page<DictionaryType> dictionaryTypeList=dictionaryTypeRepository.findPage(page,filters);
		return dictionaryTypeList;
	}
	@Transactional(readOnly = true)
	public DictionaryType findDictionaryTypeById(String id){
		DictionaryType dictionaryType=dictionaryTypeRepository.get(id);
		return dictionaryType;
	}	
	public void deleteDictionaryType(String id,String operator){
		dictionaryTypeRepository.delete(id,operator);
	}
	public void saveDictionaryType(DictionaryType dictionaryType){
		dictionaryTypeRepository.save(dictionaryType);
	}




}
